#!/usr/bin/env coffee

import {HTTP} from '@/config/site.coffee'

export json = (url)=>
  (
    await fetch url
  ).json()


user_headers = =>
  headers = new Headers()
  headers.append("user_token", localStorage.uid)
  headers


class _UserFetch
  constructor:(@ipc)->

  fetch:(url, option={})->
    option.headers = user_headers()
    if url.indexOf('.do') < 0
      url += '.do'
    r = await fetch(HTTP+url, option)
    if r.status == 401
      delete localStorage.uid
      @ipc.auto_update.relaunch()
      return
    r

  json: (url, option={})=>
    r = await @fetch(url,option)
    r.json()

export UserFetch = (ipc)=>
  new _UserFetch ipc
